import pandas as pd
import os
import subprocess # 导入 subprocess 模块

def get_file_paths():
    """
    使用 'find' 和 'sort' 命令获取当前目录及子目录下的所有 .csv 文件列表。
    """
    # 定义您指定的 shell 命令
    command = "find . -name '*.csv' | sort"
    print(f"Running command to find files: {command}")

    try:
        # 运行命令
        # - shell=True 允许我们使用管道符 |
        # - capture_output=True 捕获标准输出
        # - text=True 将输出解码为 UTF-8 字符串
        # - check=True 如果命令返回非零退出码（即出错），则引发异常
        result = subprocess.run(
            command, 
            shell=True, 
            capture_output=True, 
            text=True, 
            check=True
        )
        
        # 按行分割捕获的输出，得到文件路径列表
        file_paths = result.stdout.strip().split('\n')
        
        # 过滤掉可能的空字符串
        file_paths = [path for path in file_paths if path]
        
        print(f"Found {len(file_paths)} files.")
        return file_paths
        
    except subprocess.CalledProcessError as e:
        # 处理命令执行失败
        print(f"Error running command: {e}")
        print(f"STDERR: {e.stderr}")
        return []
    except FileNotFoundError:
        # 处理 'find' 或 'sort' 命令不存在的情况（例如在 Windows 上）
        print("Error: 'find' or 'sort' command not found.")
        print("Please ensure you are running this on a Linux/macOS environment.")
        return []
    except Exception as e:
        print(f"An unexpected error occurred while finding files: {e}")
        return []

# --- 主逻辑 ---

# 1. 动态获取文件列表
file_paths = get_file_paths()

# 2. 用于存储所有单独数据框（DataFrame）的列表
all_data_frames = []

# 3. 定义合并后输出的文件名
output_filename = 'combined_error_data.csv'

# 4. 检查是否找到了文件
if not file_paths:
    print("No files found or error in 'find' command. Exiting.")
else:
    # 5. 遍历每个文件路径
    for file_path in file_paths:
        try:
            # 6. 从路径中提取文件名 (e.g., '2020040200_24_grid.csv')
            filename = os.path.basename(file_path)
            
            # 7. 去掉文件扩展名 (e.g., '2020040200_24_grid')
            filename_stem = os.path.splitext(filename)[0]
            
            # 8. 按下划线 '_' 分割文件名
            parts = filename_stem.split('_')
            
            # 9. 检查文件名是否符合预期的 'time_fcst_hour_kind' 格式
            if len(parts) >= 3:
                time_val = parts[0]
                fcst_hour_val = parts[1]
                kind_val = parts[2]
                
                # 10. 读取 CSV 文件 (假设文件有表头)
                df = pd.read_csv(file_path)
                
                # 11. 添加新的列
                df['time'] = time_val
                df['fcst_hour'] = fcst_hour_val
                df['kind'] = kind_val
                
                # 12. 将处理后的数据框添加到列表中
                all_data_frames.append(df)
            else:
                # 如果文件名格式不正确，则打印警告并跳过
                print(f"Skipping file due to unexpected filename format: {file_path}")

        except FileNotFoundError:
            # 这种情况理论上不应发生，因为 'find' 刚找到了它
            print(f"Warning: File listed by 'find' not found: {file_path}")
        except pd.errors.EmptyDataError:
             # 处理空的CSV文件
            print(f"Skipping empty file: {file_path}")
        except Exception as e:
            # 捕获其他可能的读取错误
            print(f"Error processing file {file_path}: {e}")

    # 13. 检查是否成功处理了任何文件
    if all_data_frames:
        # 14. 将列表中的所有数据框合并为一个
        combined_df = pd.concat(all_data_frames, ignore_index=True)
        
        # 15. 调整列的顺序，将新添加的列放在最前面
        original_cols = [col for col in combined_df.columns if col not in ['time', 'fcst_hour', 'kind']]
        new_cols_order = ['time', 'fcst_hour', 'kind'] + original_cols
        combined_df = combined_df[new_cols_order]
        
        # 16. 将合并后的数据框保存到新的 CSV 文件
        combined_df.to_csv(output_filename, index=False)
        print(f"Successfully combined {len(all_data_frames)} files into {output_filename}")
    else:
        print("No valid files were processed. Output file not created.")

